package com.glory.hikvisionmobile.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.github.barteksc.pdfviewer.PDFView;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.base.BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_LoadingDialog;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_NetUtils;

import java.io.InputStream;

import butterknife.BindView;

/**
 * FileName: SopPdfActivity
 * Author: XJH
 * Date: 2020/2/6 18:47
 * Description: PDF读取界面
 * History:
 */
public class SopPdfActivity extends BaseActivity {
    @BindView(R.id.pdfView)
    PDFView pdfView;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.tvPage)
    TextView tvPage;
    private String fileName;
    private String filePath;
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private String TAG = "SopPdfActivity";
    private FreeUI_LoadingDialog dialog;
    private InputStream inputStream;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_sop_info;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        fileName = bundle.getString("fileName");
        filePath = bundle.getString("filePath");

        getPermission();
        dialog = new FreeUI_LoadingDialog();
        dialog.show(getSupportFragmentManager(),"LOADPDF");
    }

    @Override
    protected void initData() {
        FreeApi_NetUtils.getInputStreamFromUrl(filePath,new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 0){
                    inputStream = (InputStream) msg.obj;
                    setPDFView(inputStream);
                }
            }
        });

        new CountDownTimer(5000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                if (inputStream == null){
                    showToast("加载失败，请检查路径",false);
                }

                dialog.dismiss();
            }
        }.start();






    }

    private void setPDFView(InputStream inputStream) {
        pdfView.fromStream(inputStream)
                //是否允许翻页，默认是允许翻页
                .enableSwipe(true)
                //pdf文档翻页是否是垂直翻页，默认是左右滑动翻页
                .swipeHorizontal(false)
                //
                .enableDoubletap(true)
                //设置默认显示第0页
                .defaultPage(0)
                //设置加载监听
                .onLoad(nbPages -> {
                    tvPage.setText("1/" + nbPages);
                    dialog.dismiss();
                })
                //设置翻页监听
                .onPageChange((page, pageCount) -> {
                    tvPage.setText((page + 1) + "/" + pageCount);
                })
                .onError(t -> {
                    showToast("PDF加载失败",false);
                })
                // 渲染风格
                .enableAnnotationRendering(false)
                .password(null)
                .scrollHandle(null)
                // 改善低分辨率屏幕上的渲染
                .enableAntialiasing(true)
                // 页面间的间距。定义间距颜色，设置背景视图
                .spacing(0)
                .load();
    }

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };


    private void getPermission() {
        int hasWriteContactsPermission = ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE);
        if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_EXTERNAL_STORAGE)) {
                ActivityCompat.requestPermissions(this,
                        PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }

            ActivityCompat.requestPermissions(this, 
                    PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
        }

        while ((ContextCompat.checkSelfPermission(this,
                Manifest.permission.READ_EXTERNAL_STORAGE)) != PackageManager.PERMISSION_GRANTED) {
        }
    }


    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle(fileName);

        return new FreeUI_AddViewEntity("header", llMain);
    }
}
